package com.jeeplus.common.utils;

import com.jeeplus.core.persistence.dialect.db.OracleDialect;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.file.Paths;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Administrator
 * @title: CommonUtils
 * @projectName JeePlus
 * @description: TODO
 * @date 2019/10/3112:26
 */

public class CommonUtils {
    protected static Logger logger = LoggerFactory.getLogger(SqlUtils.class);
    public class fileUtils extends FileUtils{

    }
    public  class mapperUtils {
        private  StringBuffer sb = new StringBuffer();

        public  String getSql(String filePath, String xPath, String nodeName) throws Exception {
            for (Element e : XmlUtils.getListNodes(filePath, xPath)) {
                if (e.attribute("id").getValue().equals(nodeName)) {
                    if (XmlUtils.hasChild(e)) {
                        if (xPath.contains("insert")) {
                            sb.append(StringUtils.removeEnd(e.getTextTrim(), "#"));
                            sb.append("\r\n");
                        } else {
                            sb.append(StringUtils.removeEnd(e.getTextTrim(), "#"));
                            sb.append("\r\n");
                        }
                        getChildList(e);
                    }
                }
            }
            return sb.toString();
        }
        /***
         * 读取脚本
         *
         * @Author LH
         * @Description //TODO
         * @Date 15:39 2019/11/1
         * @param cls
         * @param fileName
         * @param xPath
         * @param nodeName
         * @return java.lang.String
         **/
        public String readScript(Class cls, String fileName, String xPath, String nodeName) throws IOException {
            try {
                String path = Paths.get((new File(cls.getResource("/Script/").getPath() + fileName)).getAbsolutePath(), new String[0]).toString();
                if ((new File(path)).exists()) {
                    return getSql(path, xPath, nodeName);
                }
                System.out.println(path);
                logger.error(String.format("【%s】脚本文件不存在!", new Object[]{fileName}));
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
            return "";
        }
        private void getChildList(Element e) throws Exception {
            try {
                int i = 0;
                for (Element e1 : XmlUtils.getChildList(e)) {
                    if (e1.getParent().attribute("prepend") != null) {
                        if (i > 0) {
                            sb.append(e1.attribute("prepend").getValue());
                            sb.append("\t");
                            sb.append(StringUtils.removeEnd(e1.getTextTrim(), "#"));
                            sb.append("\t");

                        } else {
                            sb.append(e1.getParent().attribute("prepend").getValue());
                            sb.append("\t");
                            sb.append(StringUtils.removeEnd(e1.getTextTrim(), "#"));
                            sb.append("\t");
                        }
                        sb.append("\r\n");
                    }
                    i++;
                    if (XmlUtils.hasChild(e1)) {
                        getChildList(e1);
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                throw new Exception(MessageFormat.format("【{0}】节点解析错误!", e.getName()));
            }
        }
    }

    /***
     * 对象转换
     *
     * @Author LH
     * @Description //TODO
     * @Date 13:08 2019/10/31
     * @return
     **/
    public static class objectUtils {

        public static Map<String, Object> convertToMap(Object obj) throws Exception {
            Map<String, Object> map = new LinkedHashMap<String, Object>();
            try {
                Class<?> clazz = obj.getClass();
                for (Field field : clazz.getDeclaredFields()) {
                    field.setAccessible(true);
                    String fieldName = field.getName();
                    Object value = field.get(obj);

                    if (value == null) {
                        value = "";
                    }
                    map.put(fieldName, value);
                }
            } catch (Exception e) {
                throw new Exception("对象转换异常");
            }
            return map;
        }
        public static Map<String,Object> getMap(Object obj) throws Exception {
            Map<String,Object> map=new HashMap<>();
            try {
                String className=obj.getClass().getName();
                Class<?> clz = Class.forName(className);
                Field[] fields = clz.getDeclaredFields();
                for (Field f : fields) {
                    if(f.getName().equals("this$0")){
                        continue;
                    }
                    f.setAccessible(true);
                    map.put(f.getName(), String.valueOf(f.get(obj)));
                }
            }
            catch (Exception e){
                e.printStackTrace();
                throw new Exception("实例转换失败!");
            }
            return map;
        }
    }
}
